//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "ConstantEstimator.h"
using namespace Cephei::QL::Models::Volatility;
#include <gen/QL/VolatilityCompositor.h>
using namespace Cephei::QL;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Volatility::CConstantEstimator::CConstantEstimator (UInt64 size) : CVolatilityCompositor(CConstantEstimator::typeid)
{
    try
    {
#ifdef HANDLE
        _phConstantEstimator = NULL;
#endif
        QuantLib::Size _size = (QuantLib::Size)ValueHelper::Convert (size); //d
        _ppConstantEstimator = new boost::shared_ptr<QuantLib::ConstantEstimator> (new QuantLib::ConstantEstimator ( _size ));
        SetVolatilityCompositor (boost::dynamic_pointer_cast<QuantLib::VolatilityCompositor> (*_ppConstantEstimator));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Volatility::CConstantEstimator::CConstantEstimator (boost::shared_ptr<QuantLib::ConstantEstimator>& childNative, Object^ owner) : CVolatilityCompositor(CConstantEstimator::typeid)
{
#ifdef HANDLE
	_phConstantEstimator = NULL;
#endif
	_ppConstantEstimator = &childNative;
    _ppVolatilityCompositor = new boost::shared_ptr<QuantLib::VolatilityCompositor> (boost::dynamic_pointer_cast<QuantLib::VolatilityCompositor> (*_ppConstantEstimator));
}
Cephei::QL::Models::Volatility::CConstantEstimator::CConstantEstimator (QuantLib::ConstantEstimator& childNative, Object^ owner) : CVolatilityCompositor(CConstantEstimator::typeid)
{
#ifdef HANDLE
	_phConstantEstimator = NULL;
#endif
	_ppConstantEstimator = new boost::shared_ptr<QuantLib::ConstantEstimator> (&childNative);
    _ppVolatilityCompositor = new boost::shared_ptr<QuantLib::VolatilityCompositor> (boost::dynamic_pointer_cast<QuantLib::VolatilityCompositor> (*_ppConstantEstimator));
    _ConstantEstimatorOwner = owner;
    _VolatilityCompositorOwner = owner;
}

Cephei::QL::Models::Volatility::CConstantEstimator::CConstantEstimator (CConstantEstimator^ copy) : CVolatilityCompositor(CConstantEstimator::typeid)
{
#ifdef HANDLE
	_phConstantEstimator = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppConstantEstimator = new boost::shared_ptr<QuantLib::ConstantEstimator> (copy->GetShared());
        _ppVolatilityCompositor = new boost::shared_ptr<QuantLib::VolatilityCompositor> (boost::dynamic_pointer_cast<QuantLib::VolatilityCompositor> (*_ppConstantEstimator));
    }
}
Cephei::QL::Models::Volatility::CConstantEstimator::CConstantEstimator (PLATFORM::Type^ t) : CVolatilityCompositor(CConstantEstimator::typeid)
{
#ifdef HANDLE
	_phConstantEstimator = NULL;
#endif
	if (!t->IsSubclassOf(CConstantEstimator::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Volatility::CConstantEstimator::CConstantEstimator (QuantLib::Handle<QuantLib::ConstantEstimator>& childNative, Object^ owner)  : CVolatilityCompositor(CConstantEstimator::typeid)
{
	_phConstantEstimator = &childNative;
	_ppConstantEstimator = &static_cast<boost::shared_ptr<QuantLib::ConstantEstimator>>(childNative.currentLink());
    _ppVolatilityCompositor = new boost::shared_ptr<QuantLib::VolatilityCompositor> (boost::dynamic_pointer_cast<QuantLib::VolatilityCompositor> (*_ppConstantEstimator));
    _ConstantEstimatorOwner = owner;
}
Cephei::QL::Models::Volatility::CConstantEstimator::CConstantEstimator (QuantLib::Handle<QuantLib::ConstantEstimator> childNative)  : CVolatilityCompositor(CConstantEstimator::typeid)
{
	_phConstantEstimator = &childNative;
	_ppConstantEstimator = &static_cast<boost::shared_ptr<QuantLib::ConstantEstimator>>(childNative.currentLink());
    _ppVolatilityCompositor = new boost::shared_ptr<QuantLib::VolatilityCompositor> (boost::dynamic_pointer_cast<QuantLib::VolatilityCompositor> (*_ppConstantEstimator));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Volatility::CConstantEstimator::CConstantEstimator (QuantLib::ConstantEstimator childNative)  : CVolatilityCompositor(CConstantEstimator::typeid)
{
#ifdef HANDLE
	_phConstantEstimator = NULL;
#endif
	_ppConstantEstimator = new boost::shared_ptr<QuantLib::ConstantEstimator> (new QuantLib::ConstantEstimator (childNative));
    _ppVolatilityCompositor = new boost::shared_ptr<QuantLib::VolatilityCompositor> (boost::dynamic_pointer_cast<QuantLib::VolatilityCompositor> (*_ppConstantEstimator));
}
#endif

Cephei::QL::Models::Volatility::CConstantEstimator::~CConstantEstimator ()
{
    if (_ppConstantEstimator != NULL)
    {
	    delete _ppConstantEstimator;
        _ppConstantEstimator = NULL;
    }
}
Cephei::QL::Models::Volatility::CConstantEstimator::!CConstantEstimator ()
{
    if (_ppConstantEstimator != NULL)
    {
	    delete _ppConstantEstimator;
    }
}
QuantLib::ConstantEstimator& Cephei::QL::Models::Volatility::CConstantEstimator::GetReference ()
{
    if (_ppConstantEstimator == NULL) throw REFNEW NativeNullException ();
	return **_ppConstantEstimator;
}
boost::shared_ptr<QuantLib::ConstantEstimator>& Cephei::QL::Models::Volatility::CConstantEstimator::GetShared ()
{
    if (_ppConstantEstimator == NULL) throw REFNEW NativeNullException ();
	return *_ppConstantEstimator;
}
QuantLib::ConstantEstimator* Cephei::QL::Models::Volatility::CConstantEstimator::GetPointer ()
{
    if (_ppConstantEstimator == NULL) throw REFNEW NativeNullException ();
	return &**_ppConstantEstimator;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ConstantEstimator>& Cephei::QL::Models::Volatility::CConstantEstimator::GetHandle ()
{
	if (_phConstantEstimator == NULL)
	{
		_phConstantEstimator = new Handle<QuantLib::ConstantEstimator> (*_ppConstantEstimator);
	}
	return *_phConstantEstimator;
}
#endif
bool Cephei::QL::Models::Volatility::CConstantEstimator::HasNative () 
{
	return (_ppConstantEstimator != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Volatility::IConstantEstimator^ Cephei::QL::Models::Volatility::CConstantEstimator_Factory::Create (UInt64 size)
{
    return REFNEW CConstantEstimator ( size);
}
